Skip to content

[bugfix] agent: avoid orphan Agent session lease on empty queue dispatch - #520

Open
yefuyou wants to merge 6 commits into
ThinkFlowLab:mainfrom
yefuyou:fix/509-empty-dispatch-lease
Open

yefuyou wants to merge 6 commits into
ThinkFlowLab:mainfrom
yefuyou:fix/509-empty-dispatch-lease

Conversation

@yefuyou

@yefuyou yefuyou commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #509

Summary

Adds regression tests and an explanatory comment for the empty-dispatch Agent lease case. The code fix itself is already on main: the #381 refactor (b9b7e587) moved start_agent_session() in Conversations.dispatch() to after the pending is None check, so an empty queue no longer creates an orphan agent: session row. This branch originally carried the same fix; after syncing with main only the items below remain.

Root cause (for reference)

Before the fix, run_chain() called Conversations.dispatch() after a successful turn, and dispatch() started a project Agent session before checking for a queued message. With no queued message it returned without any turn owning the new session, leaving an orphan agent: row until lease expiry. That row could make project rename, deletion or cleanup be rejected as if another window had the project open.

What this PR changes relative to main

  • apps/api/src/opendirector_api/agent.py: a comment above the existing start_agent_session(...) call in dispatch() stating that the lease is taken only once a queued message exists. No behavior change.
  • tests/test_assistant_dispatch_lease.py: 5 regression tests:
    • a single turn leaves no extra session row;
    • queued messages each get their own session, in order;
    • a message queued as the last turn finishes is neither lost nor leaks a row;
    • Stop in the middle of a chained run;
    • a failed dispatch write rolls back its own row without affecting other sessions.

No queue ordering, turn execution or #381 admission-lock behavior is changed.

Verification

  • The 5 new tests pass on this branch and on clean main (confirming the fix is already there). With the fix reverted in a scratch copy, 3 of them fail (single turn, queue order, late-queued message); the Stop and rollback tests guard existing behavior and do not catch that revert.
  • Locally: test_assistant_dispatch_lease.py + test_ci_layers.py 28 passed; lint-imports 4 contracts kept. The wider assistant/agent set has 4 failures that also fail on clean main (ffmpeg missing in the sandbox).
  • The test module needs no manual shard entry: since [bugfix] ci: route unlisted python test modules to default core shard #700, unlisted Python test modules are routed to the default core shard (check_test_layers.py --list-shard core lists it).
  • GitHub Release checks on head 6fff3d08: 17/17 passed.
  • Independent review: approved. Non-blocking notes: one test accepts both 200 and 202 for the late-queued case, and one has an unused variable.
  • No paid provider calls.

Related: #381, #379, #502.

@yefuyou
yefuyou marked this pull request as ready for review December 5, 2026 15:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bugfix] agent: empty queue dispatch can create an orphan Agent session lease

2 participants